﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SharedGenomics.Workbench.Core;
using SharedGenomics.Annotations;

namespace SharedGenomics.Workbench.Presenters
{
    public class KeggPathwayAnnotationMenuProvider : IAnnotationMenuProvider
    {
        public KeggPathwayAnnotationMenuProvider(CommandFactory commandFactory, IMenuFactory menuFactory)
        {
            this.CommandFactory = commandFactory;
            this.MenuFactory = menuFactory;
        }

        public IMenuItem CreateMenu(IAnnotation annotation)
        {
            KeggPathwayAnnotation keggAnnotation = (KeggPathwayAnnotation)annotation;

            //do not return a menu if there are no pathways to display
            if (keggAnnotation.Pathways.Count == 0)
                return null;

            IMenuItem menu = MenuFactory.CreateContextMenuItem();
            menu.DisplayText = "Kegg Pathways";

            foreach (var pathway in keggAnnotation.Pathways)
            {
                IMenuItem subMenuItem = this.MenuFactory.CreateContextMenuItem();
                subMenuItem.DisplayText = pathway.Name ?? "Name not found";

                string uriString = String.Format("http://www.genome.jp/dbget-bin/show_pathway?{0}+{1}", pathway.Id, pathway.KeggId);
                Uri navigationUri = new Uri(uriString);

                subMenuItem.Command = this.CommandFactory.CreateOpenUriAnnotationCommand(navigationUri, pathway);
                menu.AddSubMenu(subMenuItem);
            }

            return menu;
        }

        public Type AnnotationType
        {
            get { return typeof(KeggPathwayAnnotation); }
        }

        private CommandFactory CommandFactory { get; set; }
        private IMenuFactory MenuFactory { get; set; }
    }
}
